{% extends "base.html" %}
{%- import "header/package_navigation.html" as navigation -%}

{%- block title -%}
    {% call macros::doc_title(name=name, version=version) %}
{%- endblock title -%}

{%- block meta -%}
    <link rel="canonical" href="{{ canonical_url|safe }}" />
{%- endblock meta -%}

{%- block topbar -%}
  {%- include "rustdoc/topbar.html" -%}
{%- endblock topbar -%}

{%- block header -%}
    {# Set the active tab to the `crate` tab #}
    {% call navigation::package_navigation(metadata=metadata, active_tab="crate") %}
{%- endblock header -%}

{%- block body -%}
    <div class="container package-page-container">
        <div class="pure-g">
            <div class="pure-u-1 pure-u-sm-7-24 pure-u-md-5-24">
                <div class="pure-menu package-menu">
                    <ul class="pure-menu-list">
                        {%- if let (Some(documented), Some(total)) = (documented_items, total_items) -%}
                            {% set documented_f32 = documented as f32 %}
                            {% set total_f32 = total as f32 %}
                            {% set percent = documented_f32 * 100f32 / total_f32 %}
                            <li class="pure-menu-heading">Coverage</li>
                            <li class="pure-menu-item text-center"><b>{{ percent|round(2) }}%</b><br>
                                <span class="documented-info"><b>{{ documented }}</b> out of <b>{{ total }}</b> items documented</span>
                                {%- if let (Some(needing_examples), Some(with_examples)) = (total_items_needing_examples, items_with_examples) -%}
                                    <span class="documented-info"><b>{{ with_examples }}</b> out of <b>{{ needing_examples }}</b> items with examples</span>
                                {%- endif -%}
                            </li>
                        {%- endif -%}
                        {%- if let Some(source_size) = source_size -%}
                            <li class="pure-menu-heading">Size</li>
                            <li class="pure-menu-item">
                                <span class="documented-info">Source code size: {{(*source_size)|filesizeformat}} <span class="size" tabindex=0>
                                    {{- crate::icons::IconCircleInfo.render_solid(false, false, "") -}}
                                    <span class="info">This is the summed size of all the files inside the crates.io package for this release.</span>
                                </span>
                            </li>
                            {%- if let Some(doc_size) = documentation_size -%}
                                <li class="pure-menu-item">
                                    <span class="documented-info">Documentation size: {{(*doc_size)|filesizeformat}} <span class="size" tabindex=0>
                                        {{- crate::icons::IconCircleInfo.render_solid(false, false, "") -}}
                                        <span class="info">This is the summed size of all files generated by rustdoc for all configured targets</span>
                                </span>
                                </li>
                            {%- endif -%}
                        {%- endif -%}

                        <li class="pure-menu-heading">Links</li>
                        {# If the crate has a homepage, show it #}
                        {%- if let Some(homepage_url) = homepage_url -%}
                            <li class="pure-menu-item">
                                <a href="{{ homepage_url }}" class="pure-menu-link">
                                    {{ crate::icons::IconHouse.render_solid(false, false, "") }} Homepage
                                </a>
                            </li>
                        {%- endif -%}

                        {# If the crate has a custom doc url, show it #}
                        {%- if let Some(documentation_url) = documentation_url -%}
                            <li class="pure-menu-item">
                                <a href="{{ documentation_url }}" title="Canonical documentation" class="pure-menu-link">
                                    {{ crate::icons::IconFileLines.render_regular(false, false, "") }} Documentation
                                </a>
                            </li>
                        {%- endif -%}

                        {# If the release has a repository, show it #}
                        {%- if let Some(repository_url) = repository_url -%}
                            <li class="pure-menu-item">
                                <a href="{{ repository_url }}" class="pure-menu-link">
                                    {# If the repo link is for github or gitlab, show some stats #}
                                    {# TODO: add support for hosts besides github and gitlab (#35) #}
                                    {%- if let Some(repository_metadata) = repository_metadata -%}
                                        {{ crate::icons::IconCodeBranch.render_solid(false, false, "") }}
                                        {% if let Some(name) = repository_metadata.name %}
                                            {{name}}
                                        {% else %}
                                            Repository
                                        {% endif %}
                                        <br>
                                        {{ crate::icons::IconStar.render_solid(false, false, "left-margin") }} {{ repository_metadata.stars }}
                                        {{ crate::icons::IconCodeBranch.render_solid(false, false, "") }} {{ repository_metadata.forks }}
                                        {{ crate::icons::IconCircleExclamation.render_solid(false, false, "") }} {{ repository_metadata.issues }}

                                    {# If the repo link is unknown, just show a normal link #}
                                    {%- else -%}
                                        {{ crate::icons::IconCodeBranch.render_solid(false, false, "") }} Repository
                                    {%- endif -%}
                                </a>
                            </li>
                        {%- endif -%}

                        {# Show a link to the crate's crates.io page #}
                        <li class="pure-menu-item">
                            <a href="https://crates.io/crates/{{ name }}" class="pure-menu-link"
                                title="See {{ name }} on crates.io">
                                {{ crate::icons::IconCube.render_solid(false, false, "") }} crates.io
                            </a>
                        </li>

                        <li class="pure-menu-heading">Dependencies</li>
                        <li class="pure-menu-item">
                            <div class="pure-menu pure-menu-scrollable sub-menu">
                                <ul class="pure-menu-list">
                                    {# List all dependencies that the current release has #}
                                    {% call macros::dependencies_list(dependencies=dependencies, use_crate_details = true, if_empty="&mdash;") %}
                                </ul>
                            </div>
                        </li>

                        <li class="pure-menu-heading">Versions</li>
                        <li class="pure-menu-item">
                            <div class="pure-menu pure-menu-scrollable sub-menu">
                                <ul class="pure-menu-list">
                                    {# Display all releases of this crate #}
                                    {% call macros::releases_list(params=params, releases=releases, use_target_redirect = false, retain_fragment = false) %}
                                </ul>
                            </div>
                        </li>

                        {# Display the crate owner's profile picture and a link to their docs.rs profile #}
                        <li class="pure-menu-heading">Owners</li>
                        <li class="pure-menu-item">
                            {%- for owner in owners -%}
                                <a href="https://crates.io/users/{{ owner.0 }}">
                                    <img src="{{ owner.1 }}" alt="{{ owner.0 }}" class="owner">
                                </a>
                            {%- endfor -%}
                        </li>
                    </ul>
                </div>
            </div>

            <div class="pure-u-1 pure-u-sm-17-24 pure-u-md-19-24 package-details" id="main">
                {# If the release is not a library #}
                {%- if is_library == Some(false) -%}
                    <div class="warning">
                        {{ name }}-{{ version }} is not a library.
                    </div>

                {# If the release has been yanked and is a library #}
                {%- elif metadata.yanked.unwrap_or_default() -%}
                    <div class="warning">
                        {{ name }}-{{ version }} has been yanked.
                    </div>

                {# If the build succeeded, isn't yanked and is a library #}
                {%- elif build_status == "success" -%}
                    {# If there are no docs display a warning #}
                    {%- if !rustdoc_status.unwrap_or_default() -%}
                        <div class="warning">{{ name }}-{{ version }} doesn't have any documentation.</div>
                    {%- endif -%}

                {# If the build failed, the release isn't yanked and the release is a library #}
                {%- elif build_status == "failure" -%}
                    {# Display a warning telling the user we failed to build the docs #}
                    <div class="warning">
                        docs.rs failed to build {{ name }}-{{ version }}
                        <br>
                        Please check the
                        <a href="{{ params.builds_url() }}">build logs</a> for more information.
                        <br>
                        See <a href="/about/builds">Builds</a> for ideas on how to fix a failed build,
                        or <a href="/about/metadata">Metadata</a> for how to configure docs.rs builds.
                        <br>
                        If you believe this is docs.rs' fault, <a href="https://github.com/rust-lang/docs.rs/issues/new/choose">open an issue</a>.
                    </div>
                {%- elif build_status == "in_progress" -%}
                    <div class="info">
                        {{ crate::icons::IconGear.render_solid(false, true, "") }}
                        Build is in progress, it will be available soon
                    </div>
                {%- endif -%}

                {# If there is one, display the next most recent successful build #}
                {%- if let Some(last_successful_build) = last_successful_build -%}
                    <div class="info">
                        Visit the last successful build:
                        <a href="{{ params.clone().with_req_version(*last_successful_build).crate_details_url() }}">
                            {{ name }}-{{ last_successful_build }}
                        </a>
                    </div>
                {%- endif -%}

                {# If there's a readme, display it #}
                {%- if let Some(readme) = readme -%}
                    {{ crate::web::markdown::render(readme)|safe }}

                {# If there's not a readme then attempt to display the long description #}
                {%- elif let Some(rustdoc) = rustdoc -%}
                    {{ crate::web::markdown::render_with_default(rustdoc, "rust")|safe }}
                {%- endif -%}
            </div>
        </div>
    </div>
{%- endblock body -%}
